/*
 * Created on 2004-01-30
 */
package org.opencube.oms.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
import java.util.StringTokenizer;

import org.opencube.util.Constants;
import org.opencube.util.Util;

/**
 * @author <a href="mailto:maciek@fingo.pl">FINGO - Maciej Mroczko</a>
 * TODO: comment
 */
public class OMSProperties
{
	/* The name for the default section */
	private static final String DEFAULT_SECTION = "default";
	/* The start of the section token */
	private static final String SECTION_START = "[";
	/* The end of the section token */
	private static final String SECTION_END = "]";
	
	/* The properties section_name->properties map */
	private HashMap properties = new HashMap();
	
	/**
	 * Loads the properties from the given file 
	 * 
	 * @param file - file to read the properties from
	 * 
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public void loadFromFile( File file ) throws FileNotFoundException, IOException
	{
		loadFromString( Util.getFileAsString( file ) );
	}
	
	/**
	 * Loads the properties from the given string 
	 * 
	 * @param string - string to read the properties from
	 */
	public void loadFromString( String string )
	{
		StringTokenizer st = new StringTokenizer( string, Constants.STR_NEW_LINE );
		String currentSection = DEFAULT_SECTION;
		Properties currentProps = new Properties();
		properties.put( currentSection, currentProps );
		while( st.hasMoreTokens() )
		{
			String token = st.nextToken().trim();
			if( token.startsWith( SECTION_START ) && token.endsWith( SECTION_END ) )
			{
				currentSection = token.substring( 1, token.length() - 1 );
				currentProps = new Properties();
				properties.put( currentSection, currentProps );
			}
			else
			{
				int index = token.indexOf( Constants.STR_EQUALS );
				// if '=' found, not ant the start ( key exists ), and not at the end ( value exists )
				if( index > 0 && index != token.length() -1  )
				{
					String key = token.substring( 0, index ).trim();
					String value = token.substring( index + 1 ).trim();
					currentProps.put( key, value );
				}
			}
		}
	}
	
	/**
	 * Puts the given properties at the given section name
	 * 
	 * @param section - the section name
	 * @param props - properties to put
	 */
	public void putProperties( String section, Properties props )
	{
		properties.put( section, props );
	}
	
	/**
	 * Returns the properties under the given section name 
	 * 
	 * @param section - the section name
	 * 
	 * @return Properties - the properties under the given section name
	 */
	public Properties getProperties( String section )
	{
		return (Properties) properties.get( section );
	}
	
	/**
	 * Returns the default properties. These, which had no section header
	 * 
	 * @return Properties - the default properties ( without section header )
	 */
	public Properties getDefaultProperties()
	{
		return getProperties( DEFAULT_SECTION );
	}
}
